import "dart:developer";

import "package:dio/dio.dart";
import "package:sports_new/entity/match/hot_league_list_entity.dart";
import "package:sports_new/entity/match/match_odds_ok_entity.dart";
import "package:sports_new/entity/match/zq_match_point_entity.dart";
import "package:sports_new/entity/match/zq_data_entity.dart";
import "package:sports_new/entity/match/zq_lineup_entity.dart";
import "package:sports_new/entity/match/zq_list_entity.dart";
import "package:sports_new/entity/match/zq_match_data_entity.dart";
import "package:sports_new/entity/match/zq_match_intelligence_entity.dart";
import "package:sports_new/entity/match/zq_odds_entity.dart";
import "package:sports_new/entity/team/zq_team_detail_entity.dart";
import "package:sports_new/entity/team/zq_team_lineup_entity.dart";
import "package:sports_new/entity/team/zq_team_schedule_entity.dart";
import "package:sports_new/server/http/dio_utils.dart";
import "package:sports_new/utils/user.dart";

import "../../entity/match/secret_info_entity.dart";
import "../api_dir.dart";

class SoccerApi {
  ///足球热门、一级联赛列表
  static Future<HotLeagueListEntity?> getZqHotLeague() async {
    final result = await DioUtils.post("/${ApiDir.data}/app-do/leagues");
    if (result.statusCode == 200 && result.data["c"] == 200) {
      final data = HotLeagueListEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///获取足球全部比赛
  static Future<List<ZqListEntity>?> getZqAllList() async {
    final result = await DioUtils.post("/${ApiDir.match}/app-fb-do/matchAll");
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ZqListEntity> data = result.data["d"]
          .map<ZqListEntity>((e) => ZqListEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///获取进行中比赛
  static Future<List<ZqListEntity>?> getZqMatchBeginList() async {
    final result = await DioUtils.post("/${ApiDir.match}/app-fb-do/matchIng");
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ZqListEntity> data = result.data["d"]
          .map<ZqListEntity>((e) => ZqListEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///获取比赛赛果
  static Future<List<ZqListEntity>?> getZqMatchResultList(String time) async {
    final params = {"id": time};
    final result = await DioUtils.post("/${ApiDir.match}/app-fb-do/matchResult",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ZqListEntity> data = result.data["d"]
          .map<ZqListEntity>((e) => ZqListEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///获取足球热门比赛
  static Future<List<ZqListEntity>?> getZqHotList(String id,
      {String? teamId}) async {
    final result = await DioUtils.post("/${ApiDir.match}/app-fb-do/matchCustom",
        params: {"id": id, "teamIds": teamId});
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ZqListEntity> data = result.data["d"]
          .map<ZqListEntity>((e) => ZqListEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///获取比赛赛程
  static Future<List<ZqListEntity>?> getZqMatchScheduleList(String time) async {
    final params = {"id": time};
    final result = await DioUtils.post(
        "/${ApiDir.match}/app-fb-do/matchSchedule",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ZqListEntity> data = result.data["d"]
          .map<ZqListEntity>((e) => ZqListEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///获取足球关注比赛
  static Future<List<ZqListEntity>?> getZqMatchFocus() async {
    final result = await DioUtils.post("/${ApiDir.match}/app-fb-do/matchFocus",
        params: {"id": User.auth?.userId ?? ""});
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ZqListEntity> data = result.data["d"]
          .map<ZqListEntity>((e) => ZqListEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///球员个人数据统计
  static Future<ZqDataEntity?> getZqPlayerTec(int matchId, int playerId) async {
    final result = await DioUtils.post(
        "/${ApiDir.match}/app-match-do/player/technic",
        params: {"matchId": matchId, "playerId": playerId});
    if (result.statusCode == 200 && result.data["c"] == 200) {
      final data = ZqDataEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///比赛数据
  static Future<ZqMatchDataEntity?> getZqMatchAnalyseData(int id) async {
    final result = await DioUtils.post(
        "/${ApiDir.match}/app-match-do/analyseData",
        params: id);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      final data = ZqMatchDataEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///比赛积分
  static Future<ZqMatchPointEntity?> getZqMatchStanding(int id) async {
    final result =
        await DioUtils.post("/${ApiDir.data}/app-do/standing", params: id);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      final data = ZqMatchPointEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///比赛阵容
  static Future<List<ZqLineupEntity?>?> getZqMatchLineup(int id) async {
    final result =
        await DioUtils.post("/${ApiDir.match}/app-match-do/lineup", params: id);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      final data = ZqLineupEntity.fromJson(result.data["d"]);
      final data1 = ZqLineupEntity.fromJson(result.data["d"]["prevMessage"]);
      data1.isLastLineup = true;
      return [data1, data];
    }
    return null;
  }

  ///获取比赛重要事件
  static Future<Response?> getZqMatchEvent(int id, int flag) async {
    if (flag == 1) {
      final result = await DioUtils.post("/${ApiDir.match}/app-match-do/event",
          params: id);
      return result;
    } else {
      final result = await DioUtils.post("/${ApiDir.match}/app-match-do/lineup",
          params: id);
      return result;
    }
  }

  ///比赛详情指数
  static Future<List<ZqOddsEntity>?> getZqMatchOdds(
      int playType, int id) async {
    final params = {"playType": playType, "qxbMatchId": id};
    final result = await DioUtils.post("/${ApiDir.odds}/app-odds-do/exponent",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      try {
        List<ZqOddsEntity> data = result.data["d"]
            .map<ZqOddsEntity>((e) => ZqOddsEntity.fromJson(e))
            .toList();
        return data;
      } catch (e) {
        log(e.toString());
      }
    }
    return null;
  }

  ///比赛详情指数详情
  static Future<List<SoccerOddsDetailEntity>?> getZqMatchOddsDetail(
      int id, int companyId, int playType, String line) async {
    final params = {
      "companyId": companyId,
      "playType": playType,
      "qxbMatchId": id,
      "line": line
    };
    final result = await DioUtils.post(
        "/${ApiDir.odds}/app-odds-do/exponentInfo",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<SoccerOddsDetailEntity> data = result.data["d"]
          .map<SoccerOddsDetailEntity>(
              (e) => SoccerOddsDetailEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///比赛详情指数详情公司列表
  static Future<List<SoccerOddsCompanyEntity>?> getZqMatchOddsCompany(
      int id, int playType) async {
    final params = {"playType": playType, "qxbMatchId": id};
    final result = await DioUtils.post(
        "/${ApiDir.odds}/app-odds-do/companyArray",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<SoccerOddsCompanyEntity> data = result.data["d"]
          .map<SoccerOddsCompanyEntity>(
              (e) => SoccerOddsCompanyEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///澳客赔率
  static Future<MatchOddsOkEntity?> getZqMatchOddsOk(int matchId) async {
    final result = await DioUtils.post(
        "/${ApiDir.odds}/app-odds-do/oddsOkMatch",
        params: matchId);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      MatchOddsOkEntity data = MatchOddsOkEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///比赛信息
  static Future<Response> getZqMatchInfo(int id) async {
    return await DioUtils.post("/${ApiDir.match}/app-match-do/info",
        params: id);
  }

  ///获取比赛技术统计
  static Future<Response> getZqMatchTech(int id) async {
    return await DioUtils.post("/${ApiDir.match}/app-match-do/technic",
        params: id);
  }

  static Future<Response> getZqMatchTextLive(int id,
      {bool isAll = false, int? liveId}) async {
    return await DioUtils.post("/${ApiDir.match}/app-match-do/textLive",
        params: {"qxbId": id, "strategy": isAll ? 0 : (liveId ?? 1)});
  }

  ///球队简介
  static Future<ZqTeamDetailEntity?> getZqTeamDetail(int id) async {
    final params = {"id": id};
    final result = await DioUtils.post(
        "/${ApiDir.data}/app-team-channel-do/view",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      ZqTeamDetailEntity data = ZqTeamDetailEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///球队阵容
  static Future<List<ZqTeamLineupEntity>> getZqTeamLineup(int id) async {
    final params = {
      "id": id,
    };
    final result = await DioUtils.post(
        "/${ApiDir.data}/app-team-channel-do/lineup",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ZqTeamLineupEntity> data = result.data["d"]
          .map<ZqTeamLineupEntity>((e) => ZqTeamLineupEntity.fromJson(e))
          .toList();
      return data;
    }
    return [];
  }

  ///球队赛程年份
  static Future<List<TeamScheduleYearEntity>> getZqTeamScheduleYear(
      int id) async {
    try {
      final result = await DioUtils.post(
          "/${ApiDir.data}/app-team-channel-do/matchYear",
          params: {"id": id});
      if (result.statusCode == 200 && result.data["c"] == 200) {
        List<TeamScheduleYearEntity> data = result.data["d"]
            .map<TeamScheduleYearEntity>(
                (e) => TeamScheduleYearEntity.fromJson(e))
            .toList();
        return data;
      }
    } catch (e) {
      return [];
    }
    return [];
  }

  ///球队赛程
  static Future<ZqTeamScheduleEntity?> getZqTeamSchedule(
      int id, String year) async {
    final params = {"qxbTeamId": id, "year": year};
    final result = await DioUtils.post(
        "/${ApiDir.data}/app-team-channel-do/match",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      ZqTeamScheduleEntity data =
          ZqTeamScheduleEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///关注球队  1关注 2取消关注
  static Future<int?> focusTeam(int id, int isFocus) async {
    final params = {
      "addOrDel": isFocus,
      "list": [id],
      "userId": User.auth?.userId,
    };
    final result = await DioUtils.post("/${ApiDir.data}/app-do/teamFocusUpdate",
        params: params);
    if (result.statusCode == 200) {
      return result.data["c"];
    }
    return null;
  }

  ///足篮球情报
  static Future<ZqMatchIntelligenceEntity?> getIntelligence(
      int id, bool isZq) async {
    final result = await DioUtils.post(
        isZq
            ? "/${ApiDir.match}/app-match-do/matchRemark"
            : "/${ApiDir.basketball}/app-basketball-match-do/teamRemark",
        params: isZq ? id : {"matchId": id});
    if (result.statusCode == 200 && result.data["c"] == 200) {
      final data = ZqMatchIntelligenceEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///足篮球海外情报
  static Future<List<SecretInfoEntity>?> getSecretInfo(
      int id, bool isZq) async {
    final result = await DioUtils.post("/${ApiDir.info}/app-reveal/revealMatch",
        params: {"matchId": id, "sportsId": isZq ? 1 : 2});
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<SecretInfoEntity> data = result.data["d"]
          .map<SecretInfoEntity>((e) => SecretInfoEntity.fromJson(e))
          .toList();
      return data;
    }
    return [];
  }

  ///所有海外情报
  static Future<SecretEntity?> getSecretList(
      int type, int page, int pageSize) async {
    final params = {
      "data": {"type": type},
      "page": page,
      "pageSize": pageSize
    };
    final result = await DioUtils.post("/${ApiDir.info}/app-reveal/revealList",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      SecretEntity data = SecretEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }
}
